<?

class mixi_auth
{

    //**** Auth ****//

    private $refresh_token = 0;
    private $access_token  = 0;
    private $expire        = 0;

    public function authorize($authorization_code)
    {

        $url = 'https://secure.mixi-platform.com/2/token';
        $data = array(
            'grant_type' => 'authorization_code',
            'client_id' => CLIENT_ID,
            'client_secret' => CLIENT_SECRET,
            'code' => $authorization_code,
            'redirect_uri' => REDIRECT_URI,
        );

        $options = array('http' => array(
            'method' => 'POST',
            'content' => http_build_query($data),
        ));

        $contents = file_get_contents($url, false, stream_context_create($options));
        
        if($http_response_header[0] == 'HTTP/1.1 200 OK')
        {
            $data = json_decode($contents);
            $this->set_refresh_token($data->refresh_token);
            $this->set_expire($data->expire_in);
            $this->set_access_token($data->access_token);
            
            return true;
            //echo "OK";
        }
        else
        {
            return false;
            //echo "NG";
        }

    }
    
    private function set_refresh_token($refresh_token)
    {
        $this->refresh_token = $refresh_token;
    }
    
    public function get_refresh_token()
    {
        return $this->refresh_token;
    }

    private function set_expire($expire)
    {
        $this->expire = $expire;
    }

    public function get_expire()
    {
        return $this->expire;
    }

    private function set_access_token($access_token)
    {
        $this->access_token = $access_token;
    }
    
    public function get_access_token()
    {
        return $this->access_token;
    }
    
    public function token_refresh($refresh_token = null)
    {
        
        if(is_null($refresh_token))
            $refresh_token = $this->refresh_token;
        
        $url = 'https://secure.mixi-platform.com/2/token';

        $data = array(
            'grant_type' => 'refresh_token',
            'client_id' => 'b225fe62438fd46b986d',
            'client_secret' => 'b22cf64cfc03df7f0dc233e2cbaaf122691a509a',
            'refresh_token' => $refresh_token,
        );

        $options = array('http' => array(
            'method' => 'POST',
            'content' => http_build_query($data),
        ));

        $contents = file_get_contents($url, false, stream_context_create($options));

        if($http_response_header[0] == 'HTTP/1.1 200 OK')
        {
            $data = json_decode($contents);
            //$this->set_refresh_token($data->refresh_token);
            $this->set_access_token($data->access_token);
            
            return true;
            //echo "OK";
        }
        else
        {
            return false;
            //echo "NG";
        }
    }

    public function save_token($user_id, $access_token, $refresh_token, $expire)
    {
        $db = new DB_GEAR();
        
        //set query
        $aQuery['Type']   = 'replace into';
        $aQuery['Column'] = array(
                                  'user_id'       => $user_id,
                                  'access_token'  => $access_token,
                                  'refresh_token' => $refresh_token,
                                  'expire'        => $expire,
                                  'type'          => 'mixi'
                                  );
        $aQuery['Table']  = 'garden_auth_tbl';

        //set query
        $db->set_query($aQuery);

        //send query
        $result = $db->send_query(1);

        //close DB
        $db->close_DB();

        if($result['cnt'] == -1)
        {
            //echo 'NG';
            return false;
        }
        else
        {
            //echo 'OK';
            return true;
        }
    }

    public function load_token($user_id, $type = 'mixi')
    {
        $db = new DB_GEAR();
        
        //set query
        $aQuery['Type'] = 'select';
        $aQuery['Column'] = array('*');
        $aQuery['Table']  = 'garden_auth_tbl';
        $aQuery['Where']  = 'user_id ="'.$user_id.'" and type = "'.$type.'"';
        
        //set query
        $db->set_query($aQuery);

        //send query
        $result = $db->send_query(1);

        //close DB
        $db->close_DB();

        if($result['cnt'] == 1)
        {
            //echo 'OK';

            $this->set_refresh_token($result[1]['refresh_token']);
            $this->set_access_token($result[1]['access_token']);
            $this->set_expire($result[1]['expire']);
            return true;
        }
        else
        {
            //echo 'NG';
            return false;
        }
    }
}

?>